serializerr
Convert Errors & Objects into an easily-serialized vanilla Object.
serializerr
creates a vanilla Object
with a flattened prototype
chain & any non-enumerable properties mapped to enumerable properties.
This allows Error
objects to be serialised to JSON without losing
important data.
Installation
npm install serializerr
Usage
var wellSerializedError = JSON.parse(JSON.stringify(
serializerr(error)
))
console.log(wellSerializedError.name)
console.log(wellSerializedError.message)
console.log(wellSerializedError.stack)
Example
var serializerr = require('serializerr')
var error = new Error('an error')
var poorlySerializedError = JSON.parse(JSON.stringify(error))
console.log(poorlySerializedError.name)
console.log(poorlySerializedError.message)
console.log(poorlySerializedError.stack)
var errorObject = serializerr(error)
var wellSerializedError = JSON.parse(JSON.stringify(errorObject))
console.log(wellSerializedError.name)
console.log(wellSerializedError.message)
console.log(wellSerializedError.stack)
errorObject instanceof Error
Why
If you've ever tried to send an Error over a JSON-encoded connection
you've probably been surprised to find all the useful information is
sapped out of it; all the juicy properties like name
, message
&
stack
are non-enumerable thus they are not included in the
stringified JSON. This may be non-standard behaviour, as I could not
locate any mention in either the ES5.1 or the ES6 spec about it, but
Error properties are non-enumerable both in V8 (Chrome/io.js/Node.js) &
SpiderMonkey (Firefox).
I believe Error property non-enumerability was added as a security
measure to prevent stack traces and other sensitive information
accidentally leaking, but it's not uncommon to actually want to send
the data in Error objects over the wire.
serializerr
makes an Object suitable for serializing to & from
JSON. Not restricted to use with Errors, will work with any Object.
Notes on 'ize' vs 'ise'
Name was selected as programming world is mostly Americanised, and npm
search does not seem to do effective stemming.
This decision came with strong feelings of guilt and shame about what I thought
was blasphemous Americanised spelling, but it turns out this is a
misconception thus I am pardoned:
The -ize spelling is often incorrectly seen as an Americanism in
Britain. However, the Oxford English Dictionary (OED) recommends -ize
and notes that the -ise spelling is from French.
From Wikipedia: American and British English spelling differences
License
ISC